<template>
	<view class="content">
		<liuyuno-tabs @tabClick="tabClick" :tabData="tabData" :isFixed="isFixed" :isBrowser="isBrowser"></liuyuno-tabs>
		<custom-vertical-list :isGotopShow="isGotopShow" :listData="listData" :isBrowser="isBrowser"></custom-vertical-list>
		<custom-vertical-list-gotop :isShow="isGotopShow"></custom-vertical-list-gotop>
	</view>
</template>

<script>
export default {
	data() {
		return {
			title: 'Hello',
			tabData: [],
			listData: [],
			isFixed: true,
			isBrowser: false,
			currentTab: 0,
			isloaded: true,
			isGotopShow: false
		};
	},
	onLoad() {
		const that = this;
		if (typeof window !== 'undefined') {
			that.isBrowser = true;
		}
		that.getChannelData().then(v => {
			that.tabData = v;
			that.tabPullData(that.currentTab);
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 100,
				complete() {}
			});
		});
	},
	onReachBottom() {
		const that = this;
		that.loadMoreTimeout && clearTimeout(that.loadMoreTimeOut);
		that.loadMoreTimeOut = setTimeout(() => {
			that.tabPullData(that.currentTab, true).then();
		}, 500);
	},
	onPageScroll(e) {
		let top = e.scrollTop;
		if (top > 500) {
			this.isGotopShow = true;
		} else {
			this.isGotopShow = false;
		}
	},
	methods: {
		getChannelData() {
			let channel = this.$store.state.testData.channel;
			return new Promise((resolve, reject) => {
				resolve(channel);
			});
		},
		tabClick(e) {
			const that = this;
			that.currentTab = e;
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 100,
				complete() {
					that.tabPullData(e).then();
				}
			});
		},
		tabPullData(index, bool) {
			const that = this;
			return new Promise((resolve, reject) => {
				uni.showLoading({
					title: '努力拉取中...',
					mask: true
				});
				let data = that.$store.state.testData.data;
				if (that.isloaded) {
					that.isloaded = false;
					switch (index) {
						case 0:
							if (bool) that.listData = that.listData.concat(data[5]);
							else that.listData = data;
							break;
						case 1:
							that.listData = [];
							break;
						default:
							that.listData = [];
							break;
					}
				}
				that.loadMoreEndTimeOut && clearTimeout(that.loadMoreEndTimeOut);
				that.loadMoreEndTimeOut = setTimeout(function() {
					uni.hideLoading();
					that.isloaded = true;
					resolve();
				}, 1000);
			});
		}
	}
};
</script>

<style lang="less">
.content {
	position: relative;
	text-align: center;
	height: 400upx;
	margin-top: 0upx;
}
</style>
